﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Oracle.ManagedDataAccess.Client;

namespace WFL.RBAC.Oracle
{
    public class RoleDAL : BaseDAL, IRole
    {

        #region 接口方法

        public void Create(Role item)
        {
            OnCreate(item);
        }

        public void Delete(int id)
        {
            OnDelete(id);
        }

        public void Update(Role item)
        {
            OnUpdate(item);
        }

        public IList<Role> GetList(string name, int pageIndex, int pageSize)
        {
            return OnGetList(name, pageIndex, pageSize);
        }

        public Role Get(int id)
        {
            return OnGet(id);
        }

        public void BindCombinationRole(int id, int combinedId)
        {
            OnBindCombinationRole(id, combinedId);
        }

        public void UnbindCombinationRole(int id, int combinedId)
        {
            OnUnbindCombinationRole(id, combinedId);
        }

        public void UnbindCombinationRole(int id)
        {
            OnUnbindCombinationRole(id);
        }

        public IList<Role> GetCombinedRoleList(int id)
        {
            return OnGetCombinedRoleList(id);
        }

        public void BindPermission(Permission permission)
        {
            OnBindPermission(permission);
        }

        public void UpdatePermission(Permission permission)
        {
            OnUpdatePermission(permission);
        }

        public void UnbindPermission(int id, int resourceId)
        {
            OnUnbindPermission(id, resourceId);
        }

        public void UnbindPermission(int id)
        {
            OnUnbindPermission(id);
        }

        public IList<Permission> GetPermissionList(int id)
        {
            return OnGetPermissionList(id);
        }

        #endregion

        #region 虚方法

        protected virtual void OnCreate(Role item)
        {
            Orm.Insert<Role>(item);
        }

        protected virtual void OnDelete(int id)
        {
            Orm.Delete<Role>(id);
        }

        protected virtual void OnUpdate(Role item)
        {
            Orm.Update<Role>(item);
        }

        protected virtual IList<Role> OnGetList(string name, int pageIndex, int pageSize)
        {
            string condition = "name like :Name";
            OracleParameter[] parameters = new OracleParameter[]
            {
                new OracleParameter("Name","%"+name+"%"),
            };
            return Orm.SelectListByPage<Role>(condition, parameters, null, pageIndex, pageSize);
        }

        protected virtual Role OnGet(int id)
        {
            return Orm.Select<Role>(id);
        }

        protected virtual void OnBindCombinationRole(int id, int combinedId)
        {
            throw new NotImplementedException();
        }

        protected virtual void OnUnbindCombinationRole(int id, int combinedId)
        {
            throw new NotImplementedException();
        }

        protected virtual void OnUnbindCombinationRole(int id)
        {
            throw new NotImplementedException();
        }

        protected virtual IList<Role> OnGetCombinedRoleList(int id)
        {
            throw new NotImplementedException();
        }

        protected virtual void OnBindPermission(Permission item)
        {
            Orm.Insert<Permission>(item);
        }

        protected virtual void OnUpdatePermission(Permission item)
        {
            Orm.Update<Permission>(item);
        }

        protected virtual void OnUnbindPermission(int id, int resourceId)
        {
            string condition = "roleId=:Id and resourceId=:ResourceId";
            OracleParameter[] parameters = new OracleParameter[]
            {
                new OracleParameter("Id",id),
                new OracleParameter("ResourceId",resourceId),
            };
            Orm.Delete<Permission>(condition, parameters);
        }

        protected virtual void OnUnbindPermission(int id)
        {
            string condition = "roleId=:Id";
            OracleParameter[] parameters = new OracleParameter[]
            {
                new OracleParameter("Id",id),
            };
            Orm.Delete<Permission>(condition, parameters);
        }

        protected virtual IList<Permission> OnGetPermissionList(int id)
        {
            string condition = "roleId=:Id";
            OracleParameter[] parameters = new OracleParameter[]
            {
                new OracleParameter("roleId",id),
            };
            return Orm.SelectList<Permission>(condition, parameters, null);
        }

        #endregion

    }
}
